"""
author: liuxu
date: 2024.12.3
description:
"""
import allure
import pytest
from common.logger_ctrl import mylogger
from common.ssh_command import exec_cmd
from base_public.testbase import BasePublic


@pytest.mark.high
@allure.feature('code-object寄存器静态分析测试')
class TestCodeobjAnalyze(BasePublic):
    """测试--codeobj-analyze寄存器文件静态分析功能"""
    def _get_test_exe_path(self):
        # 编译全架构测试用例
        exe_name = self.compile_source_code(src_file_name="hipSimple/matrix.cpp", des_exe_name="matrix-O0-g-allArch",
                                            compile_paras="-O0 -g")
        return exe_name
    def test_906_codeobject(self, test_env):
        """
        分析gfx906的code-object代码
        :return:
        """
        gfx_idx = 1
        kernel_idx = 1
        # ret, output = exec_cmd(rf"echo -e '{gfx_idx}\n{kernel_idx}\n' | hipprof --codeobj-analyze {self.get_exe_abspath('matrix')}", cwd=test_env)
        # 使用管道符会使hipprof接收不到数据，将输入改为通过input参数传输
        ret, output = exec_cmd(rf"hipprof --codeobj-analyze {self._get_test_exe_path()}", input=f'{gfx_idx}\n{kernel_idx}\n', cwd=test_env)
        assert ret is True
        assert "analyze finish!" in output
        # 检测是否生成对应的html分析文件
        matching_files = list(test_env.glob(f"matrix-*-gfx{906}.out.obj.html"))
        assert len(matching_files) > 0, "Static analysis file not found"

    def test_926_codeobject(self, test_env):
        """
        分析gfx926的code-object代码
        :return:
        """
        gfx_idx = 2
        kernel_idx = 1
        ret, output = exec_cmd(rf"hipprof --codeobj-analyze {self._get_test_exe_path()}", input=f'{gfx_idx}\n{kernel_idx}\n', cwd=test_env)
        assert ret is True
        assert "analyze finish!" in output
        # 检测是否生成对应的html分析文件
        matching_files = list(test_env.glob(f"matrix-*-gfx{926}.out.obj.html"))
        assert len(matching_files) > 0, "Static analysis file not found"

    def test_928_codeobject(self, test_env):
        """
        分析gfx926的code-object代码
        :return:
        """
        gfx_idx = 3
        kernel_idx = 1
        ret, output = exec_cmd(rf"hipprof --codeobj-analyze {self._get_test_exe_path()}", input=f'{gfx_idx}\n{kernel_idx}\n', cwd=test_env)
        assert ret is True
        assert "analyze finish!" in output
        # 检测是否生成对应的html分析文件
        matching_files = list(test_env.glob(f"matrix-*-gfx{928}.out.obj.html"))
        assert len(matching_files) > 0, "Static analysis file not found"

    def test_936_codeobject(self, test_env):
        """
        分析gfx936的codeobject代码
        :return:
        """
        gfx_idx = 4
        kernel_idx = 1
        ret, output = exec_cmd(rf"hipprof --codeobj-analyze {self._get_test_exe_path()}", input=f'{gfx_idx}\n{kernel_idx}\n', cwd=test_env)
        assert ret is True
        assert "analyze finish!" in output
        # 检测是否生成对应的html分析文件
        matching_files = list(test_env.glob(f"matrix-*-gfx{936}.out.obj.html"))
        assert len(matching_files) > 0, "Static analysis file not found"